﻿package 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class Background extends MovieClip 
	{		
		public var ExitLeft:String;
		public var ExitRight:String;
		public var ExitUp:String;
		public var ExitDown:String;
		public var ExitLeftX:int;
		public var ExitLeftY:int;
		public var ExitRightX:int;
		public var ExitRightY:int;
		public var ExitUpX:int;
		public var ExitUpY:int;
		public var ExitDownX:int;
		public var ExitDownY:int;
		public var DoorExit1:String;
		public var Door1:MovieClip;
		public var RandomBattles:Boolean;
		public var AreaName:String;
		
		public var Quest:int;
		
		protected static const EventRemoveBackground = "EventRemoveBackground";
		
		public function Background() 
		{
			addEventListener(Event.REMOVED, Unload);
			addEventListener(Event.ENTER_FRAME, EnterFrame);
			addEventListener(Event.ADDED_TO_STAGE, AddedToStage);
		}

		private function Unload(event:Event):void
		{
			if (event.target.name == this.name)
			{
				//removeChildren();
				removeEventListener(Event.ENTER_FRAME, EnterFrame);
				removeEventListener(Event.ADDED_TO_STAGE, AddedToStage);
			}
		}
		
		public function GetMessage(index:int):String
		{
			return StringHelper.Empty;
		}
		
		private function removeChildren():void
		{
			if (numChildren > 0)
			{
				removeChildAt(0);
				removeChildren();
			}
		}
		
		private function AddedToStage(event:Event):void
		{
			var theGame:Game = (parent.parent as Game);
			Quest = theGame.MainInfo.Quest;
			SetInfo();
		}
		
		protected function SetInfo():void
		{
		}
		
		private function EnterFrame(event:Event):void
		{
			//this.parent is overworld
			var overworld:Overworld = (this.parent as Overworld);
			var player:OverworldPlayer = null;
			var yPadding:int = yPadding = (Constants.StageHeight - this.height);

			if (overworld != null)
			{
				player = overworld.Player;
				if (player != null)
				{
					var exitLeftX:int = (Constants.StageWidth - (player.width));
					var exitLeftY:int = player.y;
					var exitRightX:int = 0;
					var exitRightY:int = player.y;
					var exitUpX:int = player.x;
					var exitUpY:int = Constants.StageHeight;
					var exitDownX:int = player.x;
					var exitDownY:int = yPadding;
					
					if (player.x > this.width)
					{
						if (ExitRight != null)
						{
							if (ExitRightX != 0)
							{
								exitRightX = ExitRightX;
							}
							if (ExitRightY != 0)
							{
								exitRightY = ExitRightY;
							}
							overworld.AddContent(ExitRight, exitRightX, exitRightY);
						}
					}
					if (player.x < (0 - player.width))
					{
						if (ExitLeft != null)
						{
							if (ExitLeftX != 0)
							{
								exitLeftX = ExitLeftX;
							}
							if (ExitLeftY != 0)
							{
								exitLeftY = ExitLeftY;
							}
							overworld.AddContent(ExitLeft, exitLeftX, exitLeftY);
						}
					}
					if (player.y > (this.height + yPadding))
					{
						if (ExitDown != null)
						{
							if (ExitDownX != 0)
							{
								exitDownX = ExitDownX;
							}
							if (ExitDownY != 0)
							{
								exitDownY = ExitDownY;
							}
							overworld.AddContent(ExitDown, exitDownX, exitDownY);
						}
					}
					if (player.y < yPadding)
					{
						if (ExitUp != null)
						{
							if (ExitUpX != 0)
							{
								exitUpX = ExitUpX;
							}
							if (ExitUpY != 0)
							{
								exitUpY = ExitUpY;
							}
							overworld.AddContent(ExitUp, exitUpX, exitUpY);
						}
					}
					if (Door1 != null && !StringHelper.IsNullOrEmpty(DoorExit1))
					{
						if (player.hitTestObject(Door1) && Game.ZIsPressed)
						{
							overworld.AddContent(DoorExit1, 280, 362);
						}
					}
				}
			}
		}
		
	}
}